﻿using System.Data.Objects.SqlClient;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.PageActionManager;

namespace ZBJF.Warranty.BLL
{
    public partial class PageActionService
    {
        public Model.PageAction Load(int id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }

        public bool IsExistActionName(string actionName, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<PageAction>().And(o => o.ActionName.Equals(actionName));
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            return LoadEntities(expressionWhere).Any();
        }

        public bool IsExistActionCode(string actionCode, int id)
        {
            var expressionWhere = PredicateExtensions.True<PageAction>().And(o => o.ActionCode.Equals(actionCode));
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            return LoadEntities(expressionWhere).Any();
        }

        public IQueryable<PageAction> GetPagingList(BaseQueryParams model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<PageAction>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                expressionWhere =
                    expressionWhere.And(
                        o => (o.ActionCode + SqlFunctions.Space(1) + o.ActionName).Contains(model.Keyword));
            }
            return LoadPagerEntities<PageAction>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
    }
}